package com.hzy.fivepalace.db.service;

import com.hzy.fivepalace.common.util.ResponseUtil;
import com.hzy.fivepalace.db.dao.DtsSinceSiteMapper;
import com.hzy.fivepalace.db.domain.DtsSinceSite;
import com.hzy.fivepalace.db.domain.DtsSinceSiteExample;
import org.apache.commons.lang3.StringUtils;
import org.redisson.Redisson;
import org.redisson.api.RLock;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.concurrent.TimeUnit;

/**
 * 自提点服务
 */
@Service
public class DtsSinceSiteService {

    @Resource
    private DtsSinceSiteMapper sinceSiteMapper;

    @Resource
    private Redisson redisson;

    /**
     * 获取自提点信息
     *
     * @return
     */
    public Object getSinceSiteInfo() {
        DtsSinceSite sinceSite = sinceSiteMapper.selectOneByExample(new DtsSinceSiteExample());
        if (null == sinceSite) {
            sinceSite = new DtsSinceSite();
        }
        return ResponseUtil.ok(sinceSite);
    }

    /**
     * 修改自提点信息
     *
     * @param sinceSite
     * @return
     */
    public Object updSinceSite(DtsSinceSite sinceSite, Integer adminId) {
        RLock lock = redisson.getLock("admin_updSinceSite");
        if (lock.isLocked()) {
            return ResponseUtil.frequent("其他管理人员操作中,请稍后再试");
        }
        lock.lock(30, TimeUnit.SECONDS);

        if (StringUtils.isBlank(sinceSite.getName())) {
            lock.unlock();
            return ResponseUtil.fail("请填写自提点名称");
        }
        if (sinceSite.getName().length() > 300) {
            lock.unlock();
            return ResponseUtil.fail("自提点名称最多300字");
        }

        if (StringUtils.isBlank(sinceSite.getAddress())) {
            lock.unlock();
            return ResponseUtil.fail("请填写自提点地址");
        }
        if (sinceSite.getAddress().length() > 500) {
            lock.unlock();
            return ResponseUtil.fail("自提点地址最多500字");
        }

        if (StringUtils.isBlank(sinceSite.getBusinessStartTime())) {
            lock.unlock();
            return ResponseUtil.fail("请填写营业开始时间");
        }
        if (sinceSite.getBusinessStartTime().length() > 50) {
            lock.unlock();
            return ResponseUtil.fail("营业开始时间格式错误");
        }

        if (StringUtils.isBlank(sinceSite.getBusinessEndTime())) {
            lock.unlock();
            return ResponseUtil.fail("请填写营业结束时间");
        }
        if (sinceSite.getBusinessEndTime().length() > 50) {
            lock.unlock();
            return ResponseUtil.fail("营业结束时间格式错误");
        }
        sinceSite.setUpdateTime(LocalDateTime.now());
        sinceSite.setUpdateId(adminId);
        DtsSinceSite oldSinceSite = sinceSiteMapper.selectOneByExample(new DtsSinceSiteExample());
        if (null == oldSinceSite) {//不存在就新增
            sinceSiteMapper.insertSelective(sinceSite);
        } else {//存在就修改
            sinceSite.setId(oldSinceSite.getId());
            sinceSiteMapper.updateByPrimaryKeySelective(sinceSite);
        }
        lock.unlock();
        return ResponseUtil.ok("修改成功", null);
    }


}
